<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Tweets
 * @description : Tweets for mobile
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Tweets extends MOBILE_AUTH_Controller {

  public function index() {

    // Setting limit count
    $tweet_limit = $this->input->get('limit', TRUE);
    $tweet_limit = (int) (!empty($tweet_limit) && is_numeric($tweet_limit)) ? $tweet_limit : 10;
    $tweet_offset = $this->input->get('offset', TRUE);
    $tweet_offset = (int) (!empty($tweet_offset) && is_numeric($tweet_offset)) ? $tweet_offset : 0;
    $type = $this->input->get('type', TRUE);

    // Search term
    $search_term = $this->sanitize($this->input->get('search_term', TRUE));

    // Getting selected list
    if ($type == 'top') {
      $get_tweets = $this->tweet_model->get_top_tweets($tweet_limit, $tweet_offset);
      $get_tweets_count = $this->tweet_model->get_top_tweets_count(NULL);
      $tweets = $get_tweets['result'];
    } elseif ($type == 'my') {
      $get_tweets = $this->tweet_model->get_my_tweets($this->mobile_current_user['user_id'], $tweet_limit, $tweet_offset);
      $get_tweets_count = $this->tweet_model->get_my_tweets_count($this->mobile_current_user['user_id'], NULL);
      $tweets = $get_tweets['result'];
    } elseif ($type == 'new') {
      $get_tweets = $this->tweet_model->get_tweets($tweet_limit, $tweet_offset);
      $get_tweets_count = $this->tweet_model->get_tweets_count(NULL);
      $tweets = $get_tweets['result'];
    } elseif ($type == 'search' && !empty($search_term)) {
      $get_tweets = $this->tweet_model->search_tweets($search_term, $tweet_limit, $tweet_offset);
      $get_tweets_count = $this->tweet_model->search_tweets_count($search_term, NULL);
      $tweets = $get_tweets['result'];
    } else {
      $type = 'active';
      $get_tweets = $this->tweet_model->get_active_tweets($tweet_limit, $tweet_offset);
      $get_tweets_count = $this->tweet_model->get_active_tweets_count(NULL);
      $tweets = $get_tweets['result'];
    }

    $get_all_tweet_extras = $this->tweet_model->get_all_tweet_extras($this->mobile_current_user['user_id']);

    $get_all_tweet_users = $this->tweet_model->get_all_tweet_users();

    if (!empty($tweets)) {

      $data = array(
          'tweets' => $this->make_response($tweets),
          'extras' => $get_all_tweet_extras['result'],
          'top_users' => $this->make_users_response($get_all_tweet_users['result'])
      );

      $this->response['status'] = 1;
      $this->response['message'] = 'Success';
      $this->response['data'] = $data;
    } else {
      $this->response['message'] = 'No tweets';
    }
  }

  /**
   * Preparing response
   * 
   */
  private function make_response($tweets_data = array()) {

    $response = array();

    if (!empty($tweets_data)) {

      foreach ($tweets_data as $tweet) {

        $like_data = get_user_like_status('tweet', $tweet['tweet_id']);

        // Preparing
        $oneTweet = array();
        $oneTweet['tweet_id'] = $tweet['tweet_id'];
        $oneTweet['tweet_subject'] = $tweet['tweet_subject'];
        $oneTweet['tweet_message'] = $tweet['tweet_message'];
        $oneTweet['tweet_date'] = $tweet['tweet_date'];
        $oneTweet['tweet_replies_count'] = $tweet['tweet_replies_count'];
        $oneTweet['tweet_likes_count'] = $like_data['total_count'];
        $oneTweet['tweet_user_liked'] = $like_data['status'];
        $oneTweet['user_id'] = $tweet['user_id'];
        $oneTweet['user_name'] = $tweet['user_name'];
        $oneTweet['user_display_name'] = $tweet['user_display_name'];
        $oneTweet['user_profile_url'] = site_url('profile/' . $tweet['user_name']);
        $oneTweet['user_image_url'] = site_url('assets/photos/' . $tweet['user_image']);

        // Injecting
        $response[] = $oneTweet;
      }
    }
    return $response;
  }

  private function make_users_response($users_data = array()) {
    $users = array();
    if (!empty($users_data)) {
      foreach ($users_data as $user) {
        $user_details = array();
        $user_details['user_id'] = (int) $user['user_id'];
        $user_details['user_name'] = $user['user_name'];
        $user_details['user_display_name'] = $user['user_display_name'];
        $user_details['user_profile_url'] = site_url('profile/' . $user['user_name']);
        $user_details['user_image_url'] = site_url('assets/photos/' . $user['user_image']);
        if (isset($user['user_tweets_count'])) {
          $user_details['user_tweets_count'] = (int) $user['user_tweets_count'];
        }
        if (isset($user['user_replies_count'])) {
          $user_details['user_replies_count'] = (int) $user['user_replies_count'];
        }
        $users[] = $user_details;
      }
    }
    return $users;
  }

  public function __destruct() {
    echo json_encode($this->response);
  }

}

?>